//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Pool.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/Issuer.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CPool::CPool () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phPool = NULL;
#endif
        _ppPool = new boost::shared_ptr<QuantLib::Pool> (new QuantLib::Pool ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::CPool::CPool (boost::shared_ptr<QuantLib::Pool>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPool = NULL;
#endif
	_ppPool = &childNative;
    
}
Cephei::QL::Experimental::Credit::CPool::CPool (QuantLib::Pool& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPool = NULL;
#endif
	_ppPool = new boost::shared_ptr<QuantLib::Pool> (&childNative);
    
    _PoolOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CPool::CPool (CPool^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPool = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPool = new boost::shared_ptr<QuantLib::Pool> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CPool::CPool (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPool = NULL;
#endif
	if (!t->IsSubclassOf(CPool::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CPool::CPool (QuantLib::Handle<QuantLib::Pool>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPool = &childNative;
	_ppPool = &static_cast<boost::shared_ptr<QuantLib::Pool>>(childNative.currentLink());
    
    _PoolOwner = owner;
}
Cephei::QL::Experimental::Credit::CPool::CPool (QuantLib::Handle<QuantLib::Pool> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPool = &childNative;
	_ppPool = &static_cast<boost::shared_ptr<QuantLib::Pool>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CPool::CPool (QuantLib::Pool childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPool = NULL;
#endif
	_ppPool = new boost::shared_ptr<QuantLib::Pool> (new QuantLib::Pool (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CPool::~CPool ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPool != NULL)
    {
	    delete _ppPool;
        _ppPool = NULL;
    }
}
Cephei::QL::Experimental::Credit::CPool::!CPool ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPool != NULL)
    {
	    delete _ppPool;
    }
}
QuantLib::Pool& Cephei::QL::Experimental::Credit::CPool::GetReference ()
{
    if (_ppPool == NULL) throw gcnew NativeNullException ();
	return **_ppPool;
}
boost::shared_ptr<QuantLib::Pool>& Cephei::QL::Experimental::Credit::CPool::GetShared ()
{
    if (_ppPool == NULL) throw gcnew NativeNullException ();
	return *_ppPool;
}
QuantLib::Pool* Cephei::QL::Experimental::Credit::CPool::GetPointer ()
{
    if (_ppPool == NULL) throw gcnew NativeNullException ();
	return &**_ppPool;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Pool>& Cephei::QL::Experimental::Credit::CPool::GetHandle ()
{
	if (_phPool == NULL)
	{
		_phPool = new Handle<QuantLib::Pool> (*_ppPool);
	}
	return *_phPool;
}
#endif
bool Cephei::QL::Experimental::Credit::CPool::HasNative () 
{
	return (_ppPool != NULL);
}

Cephei::QL::Experimental::Credit::IPool^ Cephei::QL::Experimental::Credit::CPool::Add (String^ name, Cephei::QL::Experimental::Credit::IIssuer^ issuer)
{
    CIssuer^ _Cissuer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _name = (std::string)ValueHelper::Convert (name);
        _Cissuer = safe_cast<CIssuer^> (issuer);
        _Cissuer->Lock();
        QuantLib::Issuer& _issuer = static_cast<QuantLib::Issuer&> (_Cissuer->GetReference ()); 
    	(*_ppPool)->add ( _name,  _issuer );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cissuer != nullptr) _Cissuer->Unlock();
    }
}
Cephei::QL::Experimental::Credit::IPool^ Cephei::QL::Experimental::Credit::CPool::Clear::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppPool)->clear ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::IIssuer^ Cephei::QL::Experimental::Credit::CPool::Get (String^ name)
{
    try
    {
        std::string _name = (std::string)ValueHelper::Convert (name);
    	QuantLib::Issuer& _rv = (QuantLib::Issuer&)(*_ppPool)->get ( _name );   
        Cephei::QL::Experimental::Credit::CIssuer^ _nrv = gcnew Cephei::QL::Experimental::Credit::CIssuer (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CPool::GetTime (String^ name)
{
    try
    {
        std::string _name = (std::string)ValueHelper::Convert (name);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPool)->getTime ( _name );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Credit::CPool::Has (String^ name)
{
    try
    {
        std::string _name = (std::string)ValueHelper::Convert (name);
    	bool _rv = (bool)(*_ppPool)->has ( _name );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<String^>^ Cephei::QL::Experimental::Credit::CPool::Names::get ()
{
    try
    {
    	std::vector<std::string>& _rv = (std::vector<std::string>&)(*_ppPool)->names ( );   
        Cephei::IVector<String^>^ _nrv = gcnew CoVector<String^> (gcnew CStringVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::IPool^ Cephei::QL::Experimental::Credit::CPool::SetTime (String^ name, Double time)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _name = (std::string)ValueHelper::Convert (name);
        QuantLib::Real _time = (QuantLib::Real)ValueHelper::Convert (time);
    	(*_ppPool)->setTime ( _name,  _time );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Credit::CPool::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppPool)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IPool^ Cephei::QL::Experimental::Credit::CPool_Factory::Create ()
{
    return gcnew CPool ();
}
